import { createRouter, createWebHashHistory } from "vue-router";
import testRouter from "./testRouter";
import { useMenuStore, routerIsParamsOrQuery } from "@/store/modules/menu";

const routes = [
  {
    path: "/",
    name: "home",
    component: () => import("../views/home.vue"),
  },
  ...testRouter,
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach(async (to, from, next) => {
  if (["/login", "/home", "/"].some((it) => it === to.path)) {
    next();
    return;
  }

  // const menuStore = useMenuStore();
  // const { status, originalPath, totalPath } = routerIsParamsOrQuery(to);
//   if (isEmpty(menuStore.menuState.pagesMap)) {
//     const token = localStorage.getItem(localStorageKeys.token);
//     if (!token) {
//       message.error("登录失效，请重新登录");
//       return globalLogout();
//     }
//     // 从本地获取
//     let menuList = menuStore.getMenuList();
//     if (!menuList?.length) {
//       try {
//         menuList = await menuStore.initMenuList();
//       } catch (error) {
//         console.log(error);
//         return globalLogout();
//       }
//     }

//     let pagesMap = menuStore.menuState.pagesMap || {};
//     menuStore.menuState.tabSelect = totalPath;
//     // 携带参数，并且在路由表中
//     if (pagesMap[originalPath] && status) {
//       const routeItem = { ...pagesMap?.[originalPath] };
//       routeItem.path = totalPath;
//       menuStore.menuState.headTabNavList = [
//         ...menuStore.menuState.headTabNavList,
//         routeItem,
//       ];
//     }

//     // 不携带参数, 并且在路由表中
//     if (pagesMap?.[originalPath] && !status) {
//       menuStore.menuState.headTabNavList = [
//         ...menuStore.menuState.headTabNavList,
//         pagesMap?.[totalPath],
//       ];
//     }

//     return next();
//   }

//   // 有路由表,这里是为了兼容无界基座
//   if (!menuStore.menuState.headTabNavList?.length) {
//     menuStore.getMenuList();
//   }
  // let index = menuStore.menuState.headTabNavList?.findIndex((v) => {
  //   return v?.path?.indexOf(originalPath) >= 0;
  // });

  // 路由表存在此路由
  // if (index > -1) {
  //   menuStore.menuState.tabSelect = totalPath;
  //   menuStore.menuState.headTabNavList[index].path = totalPath;
  //   return next();
  // }

  // 设置选择的路由
//   if (
//     isEmpty(menuStore.menuState.pagesMap[originalPath]) && !window.__POWERED_BY_WUJIE__
//   ) {
//     message.error("没有相关权限,请联系管理员");
//     globalLogout();
//     return;
//   }

//   menuStore.menuState.tabSelect = totalPath;
//   // 携带参数路由参数
//   if (menuStore.menuState.pagesMap?.[originalPath] && status) {
//     const routeItem = { ...menuStore.menuState.pagesMap[originalPath] };
//     routeItem.path = totalPath;
//     menuStore.menuState.headTabNavList = [
//       ...menuStore.menuState.headTabNavList,
//       routeItem,
//     ];
//   }

//   // 没有携带路由参数
//   if (menuStore.menuState.pagesMap[originalPath] && !status) {
//     menuStore.menuState.headTabNavList = [
//       ...menuStore.menuState.headTabNavList,
//       menuStore.menuState.pagesMap[totalPath],
//     ];
//   }

  next();
});

// wujieEventBus.$on("CHANGE_ROUTE", function ({ path, query, app }) {
//   if (app !== APP_NAME_IN_WUJIE) return;
//   console.log(
//     `%c${APP_NAME_IN_WUJIE} received CHANGE_ROUTE 【path】:👉👉👉 `,
//     "background-color: #138808; font-weight: bolder; color: #fff; padding: 4px",
//     path
//   );
//   console.log(
//     `%c${APP_NAME_IN_WUJIE} received CHANGE_ROUTE 【query】:👉👉👉 `,
//     "background-color: #138808; font-weight: bolder; color: #fff; padding: 4px",
//     query
//   );
//   router.push({ path, query });
// });

export default router;
